package com.xlf.service.judge.config;

import lombok.Data;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2023/11/11 12:04
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
@Data
public class JudgeConfig {

    @Value("${judge.max}")
    private Integer max;

    /**
     * 判题沙箱url
     */
    @Value("${judge.url:http://localhost:5050}")
    private String url;

    /**
     * 自定义容器工厂，设置最大线程数
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                 ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(max);
        factory.setMaxConcurrentConsumers(max);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

}
